/**
 * 
 */
package com.xpec.c4.service.module;

import com.ms.service.module.zone.command.BasicDebugCommand;
import com.ms.service.module.zone.command.CommandHelper;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.service.common.ServiceConfig;

/**
 * @author TW15010
 *
 */
public class ModuleCommand extends BasicDebugCommand {
	private CommandHelper helper = new CommandHelper(getName());

	// 關閉 module
	static final private int CMD_SHUTDOWN = 1;
	static final private int CMD_SHUTDOWN_PARAM_MODULE_ID = 2;

	// 重啟 module
	static final private int CMD_RESTART = 2;
	static final private int CMD_RESTART_PARAM_MODULE_ID = 2;

	// 重啟 module
	static final private int CMD_GET_ALL_MODULE = 3;

	public ModuleCommand() {
		helper.addParam(CMD_SHUTDOWN, "shutdown", "[moduleId]");
		helper.addParam(CMD_RESTART, "restart", "[moduleId]");
		helper.addParam(CMD_GET_ALL_MODULE, "getall", "");
	}

	@Override
	public String execute() {
		int index = helper.getCommandIndex(commandValue);
		if (index == CommandHelper.NOT_FOUND) {
			return helper.getDescription();
		}

		switch (index) {
		case CMD_SHUTDOWN: {
			String moduleId = getParam(CMD_SHUTDOWN_PARAM_MODULE_ID);
			if (!GameUtility.isHaveString(moduleId)) {
				return getDescription();
			}

			if (!ServerProperties.isModuleNameExist(moduleId)) {
				return "module [" + moduleId + "] is not exist in " + ServiceConfig.gameInfoFileName;
			}

			ModuleServer.sendShutdownModule(moduleId);
			return "ok";
		}

		case CMD_RESTART: {
			String moduleId = getParam(CMD_RESTART_PARAM_MODULE_ID);
			if (!GameUtility.isHaveString(moduleId)) {
				return getDescription();
			}

			if (!ServerProperties.isModuleNameExist(moduleId)) {
				return "module [" + moduleId + "] is not exist in " + ServiceConfig.gameInfoFileName;
			}

			ModuleServer.sendRestartModule(moduleId);
			return "ok";
		}

		case CMD_GET_ALL_MODULE: {
			ModuleServer.sendGetAllModuleStatus();
			return "ok";
		}

		} // switch (index) {

		return getDescription();
	}

	@Override
	public String getName() {
		return "module";
	}

	@Override
	public String getDescription() {
		return helper.getDescription();
	}

	@Override
	public String getShortDescription() {
		return "module control command.";
	}
}
